//
//  WActivityIndicator.m
//
//  Created by ladaka31 ladaka on 13. 5. 31..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.

#import "CustomActIndicator.h"
#import "Sky72AppDelegate.h"

@implementation CustomActIndicator
@synthesize window = _window;

+ (CustomActIndicator *) sharedActivityIndicator {
    static CustomActIndicator *indicator = nil;
    
    if (indicator == nil) {
        @synchronized(self) {
            if (indicator == nil) {
                indicator = [[self alloc] init];
            }
        }
    }
    return indicator;
}


- (id) init {
    if (self == [super init]) {
        Sky72AppDelegate *delegate = (Sky72AppDelegate *)[UIApplication sharedApplication].delegate;
        self.window = delegate.window;

        _activityView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(self.window.center.x, self.window.center.y, _activityView.frame.size.width, _activityView.frame.size.height)];
        _activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        
        //커스텀 액티비티 백그라운드 설정
        _bgView = [[UIView alloc] init];
        _bgView.frame = [[UIScreen mainScreen] bounds];
        _bgView.backgroundColor = [UIColor clearColor];
        
        //로딩뷰 백그라운드 이미지를 넣을 때 사용
        UIImage *img = [UIImage imageNamed:@"loading_bg.png"];
        UIImageView *dimmingView = [[UIImageView alloc] initWithImage:img];
        dimmingView.alpha = 0.7;
        dimmingView.frame = CGRectMake(_bgView.center.x - img.size.width/4, _bgView.center.y- img.size.height/4, img.size.width/2, img.size.height/2);
        
        UILabel *lbl = [[UILabel alloc] init];
        lbl.text = @"로딩중...";
        lbl.textColor = [UIColor whiteColor];
        [lbl setFont:[UIFont systemFontOfSize:14]];
        lbl.textAlignment = UITextAlignmentCenter;
        lbl.backgroundColor = [UIColor clearColor];
        lbl.frame = CGRectMake(_bgView.center.x - img.size.width/4, _bgView.center.y- img.size.height/4+35, img.size.width/2, img.size.height/2);
        [_bgView addSubview:dimmingView];
      //  [_bgView addSubview:lbl];

//        _bgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        [_bgView addSubview:_activityView];
    }
    
    return self;
}

- (void) startIndicator {
    _bgView.hidden = NO;
    _activityView.hidden = NO;
    [_activityView startAnimating];
    
    [self.window addSubview:_bgView];
}

- (void) stopIndicator {
    if (_bgView != nil) {
        [_activityView stopAnimating];
        _activityView.hidden = YES;
        _bgView.hidden = YES;
    }
}

- (BOOL) isActivityAnimating {
    return _activityView.isAnimating;
}

@end
